<!DOCTYPE html>
<html lang="zh-cn"><head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/main.css" rel="stylesheet">

    <link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico" />

    <title>路由设置和route命令的使用 | wiseAI的小站</title>
</head><body><div class="container-fluid">
    <nav class="navbar fixed-top navbar-expand-sm navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <img src="/img/favicon.ico" alt="" width="30" height="24" class="d-inline-block align-text-top">
                wiseAI的小站
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarSupportedContent">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/articles/">文章</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/categories/">分类</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/tags/">关键字</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/post/golang/">golang编程</a>
                    </li>
                    
                    <li class="nav-item">
                        <a  class="nav-link"   href="/about/">关于</a>
                    </li>
                    
                </ul>
                <form class="d-flex">
                    <input id="search-query" class="form-control me-2" type="search" placeholder="Search for anything..." aria-label="Search">
                </form>
            </div>
        </div>
    </nav>
</div>




<div id="content">
<div class="container article">
	<h1>路由设置和route命令的使用</h1>
	<div class="post-meta">
		<div>
			
			
			By wiseai | 2023-02-13 | 2 minutes
		</div>
		<div class="tags">
			
			<a class="btn btn-light links btn-sm" href="/tags/linux/">Linux</a>
			
			<a class="btn btn-light links btn-sm" href="/tags/%E6%A0%91%E8%8E%93%E6%B4%BE/">树莓派</a>
			
		</div>
	</div>
	<div>
		<div class="article-post">
			<ul>
<li>命令格式：</li>
</ul>
<p><code>route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] </code></p>
<ul>
<li>命令功能：</li>
</ul>
<p>Route命令是用于操作基于内核ip路由表，它的主要作用是创建一个静态路由让指定一个主机或者一个网络通过一个网络接口，如eth0。当使用&quot;add&quot;或者&quot;del&quot;参数时，路由表被修改，如果没有参数，则显示路由表当前的内容。</p>
<ul>
<li>
<p>命令参数：</p>
<p>-c 显示更多信息</p>
<p>-n 不解析名字</p>
<p>-v 显示详细的处理信息</p>
<p>-F 显示发送信息</p>
<p>-C 显示路由缓存</p>
<p>-f 清除所有网关入口的路由表。</p>
<p>-p 与 add 命令一起使用时使路由具有永久性。</p>
<p>add:添加一条新路由。</p>
<p>del:删除一条路由。</p>
<p>-net:目标地址是一个网络。</p>
<p>-host:目标地址是一个主机。</p>
<p>netmask:当添加一个网络路由时，需要使用网络掩码。</p>
<p>gw:路由数据包通过网关。注意，你指定的网关必须能够达到。</p>
<p>metric：设置路由跳数。</p>
<p>Command 指定您想运行的命令 (Add/Change/Delete/Print)。</p>
<p>Destination 指定该路由的网络目标。</p>
<p>mask Netmask 指定与网络目标相关的网络掩码（也被称作子网掩码）。</p>
<p>Gateway 指定网络目标定义的地址集和子网掩码可以到达的前进或下一跃点 IP 地址。</p>
<p>metric Metric 为路由指定一个整数成本值标（从 1 至 9999），当在路由表(与转发的数据包目标地址最匹配)的多个路由中进行选择时可以使用。</p>
<p>if Interface 为可以访问目标的接口指定接口索引。若要获得一个接口列表和它们相应的接口索引，使用 route print 命令的显示功能。可以使用十进制或十六进制值进行接口索引。</p>
</li>
</ul>
<p>oute 命令的输出项说明:</p>
<pre><code>Destination	目标网段或者主机
Gateway	网关地址，”*” 表示目标是本主机所属的网络，不需要路由
Genmask	网络掩码
Flags	标记。一些可能的标记如下：
    U — 路由是活动的
    H — 目标是一个主机
    G — 路由指向网关
    R — 恢复动态路由产生的表项
    D — 由路由的后台程序动态地安装
    M — 由路由的后台程序修改
    ! — 拒绝路由
Metric	路由距离，到达指定网络所需的中转数（linux 内核中没有使用）
Ref	路由项引用次数（linux 内核中没有使用）
Use	此路由项被路由软件查找的次数
Iface	该路由表项对应的输出接口
</code></pre>
<ul>
<li>
<p>route 命令使用举例</p>
<ul>
<li>添加到主机的路由
<pre tabindex="0"><code># route add -host 192.168.1.2 dev eth0 
# route add -host 10.20.30.148 gw 10.20.30.40
</code></pre></li>
<li>添加到网络的路由
<pre tabindex="0"><code># route add -net 10.20.30.40 netmask 255.255.255.248 eth0   
# route add -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41 
# route add -net 192.168.1.0/24 eth1
</code></pre></li>
<li>添加默认路由
<pre tabindex="0"><code># route add default gw 192.168.1.1
</code></pre></li>
<li>删除路由
<pre tabindex="0"><code># route del -host 192.168.1.2 dev eth0:0
# route del -host 10.20.30.148 gw 10.20.30.40
# route del -net 10.20.30.40 netmask 255.255.255.248 eth0
# route del -net 10.20.30.48 netmask 255.255.255.248 gw 10.20.30.41
# route del -net 192.168.1.0/24 eth1
# route del default gw 192.168.1.1
</code></pre></li>
</ul>
</li>
<li>
<p>WINDOWS下的route命令</p>
<p>查看路由状态：routeprint</p>
<p>只查看ipv4（ipv6）路由状态：route print-4(-6)</p>
<p>添加路由：route add 目的网络 mask 子网掩码 网关——重启机器或网卡失效</p>
<p>route add 192.168.20.0 mask 255.255.255.0192.168.10.1</p>
<p>添加永久：route -p add 目的网络 mask子网掩码网关</p>
<p>route -p add 192.168.20.0 mask 255.255.255.0192.168.10.1</p>
<p>删除路由：route delete 目的网络 mask 子网掩码</p>
<p>route delete 192.168.20.0 mask255.255.255.0</p>
</li>
</ul>

		</div>
	</div>
</div>


<div class="container">
    
    <div class="row">
        
        <div class="col-5">
            <a class="page-link link-dark text-end dh" href="/post/2023.02.13-linux%E7%BD%91%E5%8D%A1%E8%AE%BE%E7%BD%AE/">
                <h5>前一篇</h5><br>
                Linux网卡设置
            </a>            
        </div>
        
        <div class="col-2">
        </div>
        
        <div class="col-5">
            <a class="page-link link-dark text-start dh" href="/post/2023.02.27-%E5%AD%97%E4%BD%93%E4%B8%8B%E8%BD%BD%E7%BD%91%E7%AB%99/">
                <h5>后一篇</h5><br>
                字体下载网站
            </a>            
        </div>
        
    </div>
    
</div>

        </div><br><br>
<footer class="container">
    <h2>友情链接</h2>
    <hr>
    <nav class="nav nav-pills flex-column flex-sm-row">
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/gnzg/index.html" target="_blank">孤鸟之歌</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/mm/index.html" target="_blank">生成随机字符</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/md/index.html" target="_blank">MarkDown</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/" target="_blank">Gitee</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://github.com/" target="_blank">Github</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyun.com/" target="_blank">阿里云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cloud.tencent.com/" target="_blank">腾讯云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.oschina.net/" target="_blank">OSCHINA开源中国</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://gitee.com/wiseai/the-way-to-go_ZH_CN/blob/master/eBook/directory.md" target="_blank">the way to go</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://topgoer.com/" target="_blank">golang文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://goframe.org/display/gf" target="_blank">GoFrame</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.aliyundrive.com/" target="_blank">阿里云盘</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://cn.vuejs.org/" target="_blank">vue3文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://element-plus.gitee.io/zh-CN/" target="_blank">element-plus文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.runoob.com/vue3/vue3-tutorial.html" target="_blank">vue3菜鸟教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://v5.bootcss.com/" target="_blank">bootstrap v5 中文文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.bootstrap.cn/" target="_blank">bootstrap文档</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://caddy2.dengxiaolong.com/docs/" target="_blank">caddy中文教程</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.58pic.com/" target="_blank">千图</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://ifonts.com/" target="_blank">iFonts</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://marketing.qiniu.com/" target="_blank">七牛云</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.ixigua.com/" target="_blank">西瓜视频</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/yugang/index.html" target="_blank">鱼缸计算</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://justcc.mengkang.net/#/" target="_blank">C语言JustCC</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://wiseai.gitee.io/pages/pptist/" target="_blank">PPTist</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="/index.xml" target="_blank">RSS</a>
        
        <a class="flex-sm-fill text-sm-center nav-link links link-dark" href="https://www.kancloud.cn/idcpj/python/418553" target="_blank">参考资料</a>
        
    </nav>

    <div class="copyright text-center">
      <span class="power-by">
        Powered by <a class="links" href="https://gohugo.io" target="_blank">Hugo</a>
    </span>
    <span>|</span>
    <span>
        Theme - <a class="links" href="https://github.com/wiseai-go/blog-hugo" target="_blank">WiseAI</a>
    </span>
    <br>
    <span class="copyright-year">
        &copy;
        
        2017 -
        2023<span>
            陇ICP备15000157号
            
        </span></span>

</div>
</footer>
<script src="/js/bootstrap.bundle.min.js"></script>


</body>
</html>
